﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crownwood.Magic.Docking;
using ConnectSQLServer.Controls;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using ConnectSQLServer.Common;

namespace ConnectSQLServer.Manager
{

    /// <summary>
    /// Summary description for DBExplorerDocking.
    /// </summary>
    public class DatabaseExplorerManager
    {
        private DockingManager dockingManager;
        private DatabaseExplorer databaseExplorer;
        private Content content;
        private Server server;
        private List<ServerConnection> lsServerConnection;

        public DatabaseExplorer DATABaseExplorer
        {
            get
            {
                return this.databaseExplorer;
            }
        }

        public DockingManager DOCKINGManager
        {
            get
            {
                return this.dockingManager;
            }
        }

        public Content CONTENT
        {
            get
            {
                return this.content;
            }
        }

        public DatabaseExplorerManager(ContainerControl container)
        {
            //add docking 
            this.dockingManager = new DockingManager(container, Crownwood.Magic.Common.VisualStyle.IDE);
            this.databaseExplorer = new DatabaseExplorer();
            this.content = new Content(this.dockingManager);
            this.content.Control = this.databaseExplorer;
            this.content.Title = "Database Explorer";
            this.content.FullTitle = "Database Explorer";
            this.content.HideButton = false;
            this.content.CloseButton = false;
            this.content.DisplaySize = new System.Drawing.Size(container.Size.Width, container.Size.Height);
            this.dockingManager.Contents.Add(this.content);
            this.dockingManager.ShowContent(this.content);
            this.lsServerConnection = new List<ServerConnection>();

            //add Event Handle
            this.databaseExplorer.treeViewDB.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(TreeView_MouseDoubleClick);
        }

        public void AddConnection(Server s, ServerConnection sc)
        {
            bool flag = false;
            server = s;
            this.server.ConnectionContext.Connect();

            foreach (ServerConnection scon in lsServerConnection)
            {
                if (scon.DatabaseName == sc.DatabaseName)
                {
                    flag = true;
                    break;
                }
            }
            if (flag == false)
            {
                lsServerConnection.Add(sc);
                if (lsServerConnection.Count == 1)
                    CreateTreeView();
                else
                    UpdateTreeView(sc);
            }
        }

        public void CreateTreeView()
        {
            this.databaseExplorer.treeViewDB.Nodes.Clear();
            this.databaseExplorer.treeViewDB.Nodes.Add("Data Connection");
            this.databaseExplorer.treeViewDB.Nodes[0].ImageIndex = 0;
            this.databaseExplorer.treeViewDB.Nodes[0].SelectedImageIndex = 0;
            UpdateTreeView(lsServerConnection[lsServerConnection.Count - 1]);
        }

        public void UpdateTreeView(ServerConnection sc)
        {

            int index = lsServerConnection.Count - 1;

            foreach (Database db in this.server.Databases)
            {
                if (db.Name != null && db.Name == sc.DatabaseName)
                {
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes.Add(db.Name);

                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Tag = db.ID.ToString();

                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes.Add("Tables");
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes.Add("Procedures");
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].ContextMenuStrip = GlobalVariable.contextMenuDB;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].ImageIndex = 1;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].SelectedImageIndex = 1;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].ImageIndex = 4;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].SelectedImageIndex = 4;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].ImageIndex = 4;
                    this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].SelectedImageIndex = 4;
                }
            }
            this.databaseExplorer.treeViewDB.Nodes[0].ContextMenuStrip = GlobalVariable.contextMenuDBS;
            int j;
            try
            {
                j = -1;
                foreach (Microsoft.SqlServer.Management.Smo.Table tb in this.server.Databases.ItemById(Convert.ToInt32(this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Tag)).Tables)
                {
                    if (tb.Name != null && !tb.IsSystemObject)
                    {
                        j++;

                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].Nodes.Add(tb.Name);

                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].Nodes[j].Tag = tb.ID.ToString();
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].Nodes[j].ImageIndex = 2;
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].Nodes[j].SelectedImageIndex = 2;
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[0].Nodes[j].ContextMenuStrip = GlobalVariable.contextMenuTable;
                    }
                }
            }
            catch (System.Exception)
            {

            }
            try
            {
                j = -1;
                foreach (Microsoft.SqlServer.Management.Smo.StoredProcedure sp in this.server.Databases.ItemById(Convert.ToInt32(this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Tag)).StoredProcedures)
                {
                    if (sp.Name != null && !sp.IsSystemObject)
                    {
                        j++;
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].Nodes.Add(sp.Name);
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].Nodes[j].Tag =sp.ID.ToString(); 
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].Nodes[j].ImageIndex = 3;
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].Nodes[j].SelectedImageIndex = 3;
                        this.databaseExplorer.treeViewDB.Nodes[0].Nodes[index].Nodes[1].Nodes[j].ContextMenuStrip = GlobalVariable.contextMenuStored;
                    }
                }
            }
            catch (System.Exception)
            {
            }

            this.databaseExplorer.treeViewDB.Nodes[0].Expand();
        }

        public void RefreshTreeView()
        {
            this.databaseExplorer.treeViewDB.Nodes.Clear();
            foreach (ServerConnection sc in lsServerConnection)
            {
                UpdateTreeView(sc);
            }
        }

        public void ShowTableDesign(InfoDatabase info)
        {
            GlobalVariable.TManager.ShowTableDesign(info);
        }

        private bool CheckTabPageExisted(string name)
        {
            bool flag = false;

            return flag;
        }

        //Event Handle

        public void TreeView_MouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Parent!=null)
            {
                if (e.Node.Parent.Text == "Tables")
                {
                    InfoDatabase info = new InfoDatabase();
                    info.SERVER = this.server;
                    info.TableName = e.Node.Text;
                    info.DatabaseName = e.Node.Parent.Parent.Text;
                    info.DatabaseID = Convert.ToInt32(e.Node.Parent.Parent.Tag);
                    info.TableID = Convert.ToInt32(e.Node.Tag);
                    this.ShowTableDesign(info);
                }

                if (e.Node.Parent.Text == "Procedures")
                {
                    InfoDatabase info = new InfoDatabase();
                    info.SERVER = this.server;
                    info.StoredName = e.Node.Text;
                    info.DatabaseName = e.Node.Parent.Parent.Text;
                    info.DatabaseID = Convert.ToInt32(e.Node.Parent.Parent.Tag);
                    info.StoredID = Convert.ToInt32(e.Node.Tag);
                    MessageBox.Show(info.StoredName + info.DatabaseName);
                }
            }
        }
    }
}
